class Creature
  def initialize(name, char, color)
    @x, @y = $map_width / 2, $map_height / 2
    @dx, @dy = 0, 0
    @str, @int, @dex = 4, 3, 4
    @hp, @mp, @spd = @str * 5, @int * 5, @dex * 5
    @name, @char, @color = name, char, color
    @myturn, @dead, @noclip = true, false, false
    @turns = 1
  end
  def getx
    return @x
  end
  def gety
    return @y
  end
  def getname
    return @name
  end
  def getchar
    return @char
  end
  def getcolor
    return @color
  end
  def getstr
    return @str
  end
  def getdex
    return @dex
  end
  def gethp
    return @hp
  end
  def subhp(points)
    @hp = self.gethp - points
  end
  def getmp
    return @mp
  end
  def turn?
    return @myturn
  end
  def turns?
    return @turns
  end
  def endturn
    @myturn = false
    @turns += 1
  end
  def beginturn
    @myturn = true
  end
  def dead?
    return @dead
  end
  def noclip?
    return @noclip
  end
  def noclip(bool)
    @noclip = bool
  end
  def kill
    @dead = true
    $currentmap.frecth(self.getx - 1, self.gety - 1, self.getx + 2, self.gety + 2, $blood)
  end
  def hit(target)
    if @dex > rand(5)
      dmg = (self.getstr - target.getstr) + rand(5)
      target.subhp(dmg)
      $currentmap.addthing(target.getx, target.gety, $blood)
      $msgbox.say("#{self.getname} hits #{target.getname} for #{dmg} damage.")
    else
      $msgbox.say("#{self.getname} misses #{target.getname}")
    end
    self.endturn
  end
  def draw
    setpos @y, @x
    if self.getcolor > 6
      attron(A_BOLD)
    end
    attron(color_pair(self.getcolor))
    addch self.getchar
    attroff(color_pair(self.getcolor))
    attroff(A_BOLD)
  end
  def opendoor
    $msgbox.say("Which direction?")
    case getch
      when 'h'
        dx, dy = -1, 0
      when 'j'
        dx, dy = 0, 1
      when 'k'
        dx, dy = 0, -1
      when 'l'
        dx, dy = 1, 0
      else
        $msgbox.say("Invalid direction.")
    end
    if dx
      if $currentmap.getname(@x+dx, @y+dy) == "Door"
        $currentmap.addthing(@x+dx, @y+dy, $opendoor)
        $msgbox.say("You open the door.")
        self.endturn
      else
        $msgbox.say("There's nothing there to open.")
      end
    end
  end
  def closedoor
    $msgbox.say("Which direction?")
    case getch
      when 'h'
        dx, dy = -1, 0
      when 'j'
        dx, dy = 0, 1
      when 'k'
        dx, dy = 0, -1
      when 'l'
        dx, dy = 1, 0
      else
        $msgbox.say("Invalid direction.")
    end
    if dx
      if $currentmap.getname(@x+dx, @y+dy) == "Open door"
        $currentmap.addthing(@x+dx, @y+dy, $door)
        $msgbox.say("You close the door.")
        self.endturn
      else
        $msgbox.say("There's nothing there to close.")
      end
    end
  end
  def move(dx, dy)
    @dx, @dy = dx, dy
    # moving right a screen
    if @x+@dx >= $map_width
      $currentmap.save
      if $currentmap.getx + 1 < 32
        newx, newy = $currentmap.getx + 1, $currentmap.gety
        $currentmap.loadmap(newx, newy)
        self.warp(0, self.gety)
      end
      @dx, @dy = 0, 0
    end
    # moving down a screen
    if @y+@dy >= $map_height
      $currentmap.save
      if $currentmap.gety + 1 < 32
        newx, newy = $currentmap.getx, $currentmap.gety + 1
        $currentmap.loadmap(newx, newy)
        self.warp(self.getx, 0)
      end
      @dx, @dy = 0, 0
    end
    # moving left a screen
    if @x+@dx < 0
      $currentmap.save
      if $currentmap.getx - 1 >= 0
        newx, newy = $currentmap.getx - 1, $currentmap.gety
        $currentmap.loadmap(newx, newy)
        self.warp($map_width - 1, self.gety)
      end
      @dx, @dy = 0, 0
    end
    # moving up a screen
    if @y+@dy < 0
      $currentmap.save
      if $currentmap.gety - 1 >= 0
        newx, newy = $currentmap.getx, $currentmap.gety - 1
        $currentmap.loadmap(newx, newy)
        self.warp(self.getx, $map_height - 1)
      end
      @dx, @dy = 0, 0
    end
    # simple collision detection
    if $currentmap.getchar(@x+@dx,@y+@dy) == '.' or @noclip
      @x += @dx
      @y += @dy
      self.endturn
    end
    @dx, @dy = 0, 0
  end
  def warp(x, y)
    @x, @y = x, y
  end
end